package com.ux.base
{
	import com.adobe.images.BMPEncoder;
	import com.adobe.images.JPGEncoder;
	import com.adobe.images.PNGEncoder;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import mx.graphics.codec.UploadPostHelper;
	
	public class BUploadToImg extends EventDispatcher
	{
		public static const JPEG : String = "jpegType";
		public static const PNG : String = "pngType";
		public static const BMP : String = "bmpType";
		
		private var completeHandler : Function;
		private var _data : String;
		
		public function BUploadToImg( _bitmap:Bitmap, url:String, _type:String, _params:Object=null, onCompleteHandler:Function=null )
		{
			super();
			
			var byteArray:ByteArray;
			if( _type == JPEG )
			{
				byteArray = new JPGEncoder(100).encode(_bitmap.bitmapData);
			}
			else if( _type == PNG )
			{
				byteArray = PNGEncoder.encode(_bitmap.bitmapData);
			}
			else
			{
				byteArray = BMPEncoder.encode( _bitmap.bitmapData );
			}
			
			completeHandler = onCompleteHandler;
			
			var urlRequest:URLRequest = new URLRequest();
			urlRequest.url = url;
			urlRequest.method = URLRequestMethod.POST;
			
			var parameters:Object = _params;
			
			urlRequest.data = UploadPostHelper.getPostData("image.jpg", byteArray, parameters );
			urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
			urlRequest.requestHeaders.push( new URLRequestHeader( 'Content-Type', 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary() ) );
			
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			
			urlLoader.addEventListener(Event.COMPLETE, onComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			urlLoader.load(urlRequest);
		}
		
		
		/**
		 * 
		 * @param e
		 * 
		 */
		private function onError( e:IOErrorEvent ):void
		{
			trace( "업로드 에러입니다. Type : " + e.type, "  " + e.text );
			ExternalInterface.call( "alert", "이미지 업로드 에러입니다." );
			
			
			var d : Date = new Date();
			
			var param : URLVariables = new URLVariables();
			param.log = e.text;
			param.time = d.toDateString() + " " + d.toTimeString();
			
			var req : URLRequest = new URLRequest();
			req.url = "http://winnie.nafree.net/flashData/errorInsert.php";
			req.data = param;
			
			var ldr : URLLoader = new URLLoader();
			ldr.load( req );
			
			
		}
		
		private function onComplete( e:Event ):void
		{
			this._data = String( ( e.target as URLLoader ).data );
			if( completeHandler != null ) completeHandler();
			
		}
		
		public function get data():String
		{
			return _data;
		}
	}
}











